package com.ltu.m7011e.imagine.arithmetics.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.ltu.m7011e.imagine.arithmetics.client.Game;

public class Arithmetics extends Game implements EntryPoint
{
	private AsyncCallback<String> callback;
	private ArithmeticsServiceAsync reverserService;
	
	private VerticalPanel score = new VerticalPanel();
	private VerticalPanel container = new VerticalPanel();
	private Label title;
	private TextBox answer;
	private Button send;
	
	public Arithmetics()
	{
		DOM.setElementAttribute(container.getElement(), "id", "game");
		
		HorizontalPanel panel = new HorizontalPanel();
		panel.addStyleName("arithmetics");
		
		setUpRPC();
		
		Button start = new Button("Starta");
		start.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				reverserService.getQuestion("start,new", callback);
			}
		});
		panel.add(start);
		container.add(panel);
		
		score.add(timeLabel);
		score.add(attemptsLabel);
		score.add(successesLabel);
		score.add(failedLabel);
		
		gameContainer.add(score);
		gameContainer.add(container);
	}
	
	public void onModuleLoad()
	{
	}
	
	protected void result(String data)
	{
		String[] split = data.split(",");
		
		VerticalPanel panel = new VerticalPanel();
		HorizontalPanel row = new HorizontalPanel();
		row.addStyleName("questions");
		
		if(split[0].compareTo("question") == 0)
		{
			if(split.length == 3)
			{
				attempts++;
				updateAttempts();
				
				if(split[2].compareTo("true") == 0)
				{
					successes++;
					updateSuccesses();
				}
				else
				{
					failed++;
					updateFailed();
				}
			}
			
			title = new Label("Question");
			send = new Button("Svara");
			
			Label question = new Label(split[1]);
			answer = new TextBox();
			answer.addKeyDownHandler(new KeyDownHandler()
			{
				public void onKeyDown(KeyDownEvent event)
				{
					if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
					{
						send.click();
					}
				}
				
			});
			row.add(question);
			row.add(answer);
			
			send.addClickHandler(new ClickHandler()
			{
				public void onClick(ClickEvent event)
				{
					String value = answer.getValue();
					
					try
					{
						if(0 < Integer.parseInt(value))
						{
							reverserService.getQuestion("answer," + value, callback);
						}
					}
					catch(NumberFormatException e)
					{
					}
				}
			});
			
			panel.add(title);
			panel.add(row);
			panel.add(send);
		}
		else
		{
			title = new Label("Error");
			Label error = new Label(data);
			
			panel.add(title);
			panel.add(error);
		}
		
		container.clear();
		container.add(panel);
	}
	
	private void setUpRPC()
	{
		reverserService = (ArithmeticsServiceAsync) GWT.create(ArithmeticsService.class);
		((ServiceDefTarget) reverserService).setServiceEntryPoint(GWT.getModuleBaseURL() + "Games/ArithmeticsService");
		callback = new AsyncCallback<String>()
		{
			// This method will be called if the service call fails   
			public void onFailure(Throwable caught)
			{
				// Show a message informing the user why the call failed   
				Window.alert("Failed to get response from server" + caught.getMessage());
			}
			
			// This method will be called if the service call succeeds   
			public void onSuccess(String data)
			{
				// Get the service call result and cast it to the desired type and display it
				result(data);
			}
		};
	}
}
